﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.EntityClient;
using CongregacaoOs.Data;

namespace CongregacaoOs.Business
{
    [System.ComponentModel.DataObject]
    public class ccAdmSetorBus
    {

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
        public bool AdmSetorSaveOrUpdate(ccAdmSetor admSetor)
        {
            if (admSetor.idSetor == 0) { throw new Exception("O Setor não foi informado"); }
            using (EntityConnection conn = new EntityConnection("name = CcbOrdemServico"))
            {
                try
                {
                    using (EntityCommand cmd = conn.CreateCommand())
                    {


                        cmd.CommandText = "CcbOrdemServico.ccAdmSetorSave";
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;

                        EntityParameter param;

                        param = cmd.Parameters.Add("IDADMSETOR", DbType.Int32);
                        param.Direction = ParameterDirection.Input;
                        if (admSetor.idAdmSetor != 0)
                            param.Value = admSetor.idAdmSetor;
                        else
                            param.Value = DBNull.Value;

                        param = cmd.Parameters.Add("IDADM", DbType.String);
                        param.Direction = ParameterDirection.Input;
                        if (admSetor.idAdm != 0)
                            param.Value = admSetor.idAdm;
                        else
                            param.Value = DBNull.Value;

                        param = cmd.Parameters.Add("IDSETOR", DbType.Int32);
                        param.Direction = ParameterDirection.Input;
                        if (admSetor.idSetor != 0)
                            param.Value = admSetor.idSetor;
                        else
                            param.Value = DBNull.Value;

                        using (conn)
                        {
                           if(conn.State == ConnectionState.Closed) conn.Open();
                            cmd.ExecuteNonQuery();                        
                            return true;
                        }
                    }
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public List<ccAdmSetor> AdmSetorSelect(ccAdmSetor admSetor)
        {
            using (EntityConnection conn = new EntityConnection("name = CcbOrdemServico"))
            {
                try
                {
                    using (EntityCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = "CcbOrdemServico.ccAdmSetorSelect";
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;

                        EntityParameter param;

                        param = cmd.Parameters.Add("IDADMSETOR", DbType.Int32);
                        param.Direction = ParameterDirection.Input;
                        if (admSetor.idAdmSetor != 0)
                            param.Value = admSetor.idAdmSetor;
                        else
                            param.Value = DBNull.Value;

                        param = cmd.Parameters.Add("IDADM", DbType.String);
                        param.Direction = ParameterDirection.Input;
                        if (admSetor.idAdm != 0)
                            param.Value = admSetor.idAdm;
                        else
                            param.Value = DBNull.Value;

                        param = cmd.Parameters.Add("IDSETOR", DbType.Int32);
                        param.Direction = ParameterDirection.Input;
                        if (admSetor.idSetor != 0)
                            param.Value = admSetor.idSetor;
                        else
                            param.Value = DBNull.Value;



                        using (conn)
                        {
                           if(conn.State == ConnectionState.Closed) conn.Open();
                            using (EntityDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
                            {

                                List<ccAdmSetor> _result = new List<ccAdmSetor>();
                                while (reader.Read())
                                {
                                    ccAdmSetor _admSetor = new ccAdmSetor();
                                    _admSetor.idAdmSetor = reader.IsDBNull(reader.GetOrdinal("idAdmSetor")) == false ? reader.GetInt32(reader.GetOrdinal("idAdmSetor")) : 0;
                                    _admSetor.idAdm = reader.IsDBNull(reader.GetOrdinal("idAdm")) == false ? reader.GetInt32(reader.GetOrdinal("idAdm")) : 0;
                                    _admSetor.idSetor = reader.IsDBNull(reader.GetOrdinal("idSetor")) == false ? reader.GetInt32(reader.GetOrdinal("idSetor")) : 0;
                                    _result.Add(_admSetor);
                                }


                                return _result;
                            }
                        }


                    }
                }
                catch (Exception)
                {
                    return null;
                }

            }
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Delete, true)]
        public void AdmSetorDelete(int id)
        {
            try
            {
                using (CcbOrdemServico objDB = new CcbOrdemServico())
                {
                    objDB.ccAdmSetorDelete(id);
                }
            }
            catch (System.Exception)
            {

                throw;
            }
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Delete, false)]
        public void AdmSetorDeleteByAdm(int aIdAdm)
        {
            try
            {
                using (CcbOrdemServico objDB = new CcbOrdemServico())
                {
                    objDB.ccAdmSetorDeleteByAdm(aIdAdm);
                }
            }
            catch (System.Exception)
            {

                throw;
            }
        }
    }
}
